package com.hp.ams.ongoing.netatendimento.infrastructure.persistence;

import java.net.UnknownHostException;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import com.hp.ams.ongoing.netatendimento.model.entity.Cidade;
import com.mongodb.BasicDBObject;
import com.mongodb.DB;
import com.mongodb.DBCollection;
import com.mongodb.DBCursor;
import com.mongodb.Mongo;
import com.mongodb.MongoException;

public class TesteMongo {
	
	static String[] grupos = {"lstStatusProposta"};

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		
		try {
			// connect to mongoDB, ip and port number
			Mongo mongo = new Mongo("localhost", 27017);
 
			// get database from MongoDB,
			// if database doesn't exists, mongoDB will create it automatically
			DB db = mongo.getDB("netincidentes");
			//boolean auth = db.authenticate("admin", "l2M25TIwCMTE".toCharArray());
			
			//System.out.println("Autenticado : "+auth);
			
			// Get collection from MongoDB, database named "yourDB"
			// if collection doesn't exists, mongoDB will create it automatically

			DBCollection collection = db.getCollection("netanaly.cidades");
 
//			for (int i = 0; i < 3; i++) {
//				
//			
//			// create a document to store key and value
//			BasicDBObject document = new BasicDBObject();
//			document.put("id_script_execucao", i);
//			document.put("ds_script_execucao", "verifica o status da proposta");
//			document.put("corpo_script", "select * from cp_proposta");
//			document.put("qtd_parametro", 2);
//			document.put("id_grupo_execucao", 1);
//			document.put("id_ordem", 1);
//			document.put("id_causa", 1);
//		
//			
//			JdbcRepository rep = new JdbcRepository();
//			List l = rep.findAll();
//			for (Iterator iterator = l.iterator(); iterator.hasNext();) {
//				Cidade object = (Cidade) iterator.next();
//				System.out.println(object);		
//			
//			
//			BasicDBObject document = new BasicDBObject();
//			document.put("alias_base", object.getAliasBase());
//			document.put("ci_nome", object.getNomeCidade());
//			document.put("cid_contrato", object.getCidContrato());
//			document.put("cod_operadora", object.getCodOperadora());
//			document.put("id_empresa", object.getIdEmpresa());
//			// save it into collection named "yourCollection"
//			collection.insert(document);
//			}
			
			Integer retorno = 4;//rep.selectUniqueResult(scriptExecucao.getCorpoScript(), parametros);		
			Map<String, Object> parametros = new HashMap<String, Object>();
			//parametros.put("id_script_vinculado", 2);
			//parametros.put("valorRetorno", null);
			parametros.put("alias_base","db09");
			
			//collection.update(q, o);
			
			BasicDBObject newDocument3 = new BasicDBObject().append("$set", 
					new BasicDBObject().append("alias_base", "baseBrasil"));
			
			collection.update(new BasicDBObject().append("alias_base", "db09"), newDocument3);


			// search query
			BasicDBObject searchQuery = bindParameters(parametros);		
 
			// query it
			DBCursor cursor = collection.find(searchQuery);
 
			// loop over the cursor and display the retrieved result
			while (cursor.hasNext()) {
				System.out.println(cursor.next());
				
			}
 
			System.out.println("Done");
 
		} catch (UnknownHostException e) {
			e.printStackTrace();
		} catch (MongoException e) {
			e.printStackTrace();
		}
 
	}

	public static BasicDBObject bindParameters(Map<String,Object> parameters){
		BasicDBObject objectQuery = new BasicDBObject();
		for (Map.Entry<String, Object> entry : parameters.entrySet()) {
			objectQuery.put(entry.getKey(), entry.getValue());
		}
		System.out.println("Parametros montados : "+ objectQuery);
		return objectQuery;
	}

}
